<?php
class ConfigsController extends AppController {
	public $name = 'Configs';	
	public $uses=array('Config');
	function admin_index(){
		//init session
		//khoi tao cac session se dung trong 1 model dung de luu lai page, sort, filter, condition ...
		if(!$this->Session->check($this->uses[0].'2')){
			$tmp=array(
				'limit'=>$this->webConfig['default_row_per_page'],
				'conditions'=>array('id <>'=>1),
				'fields'=>null,
				'order'=>'lft',
				'page'=>1,
				'recursive'=>0,
				'sortField'=>null,
				'sortDir'=>'asc',				
				'filterField'=>null,
				'filterText'=>null,
				'catid'=>'',
			);
			//luu vao session			
			$this->Session->write($this->uses[0].'2',$tmp);			
		}	
		//get filter field
		//filterField dùng cho combobox search của trang liet ke danh sach, khai bao cac field co the search cua danh sach. 
		$filterField=array(				
			//$this->uses[0].'.key'=>_resource('config key',true),
			//$this->uses[0].'.value'=>_resource('config value',true),
			//$this->uses[0].'.description'=>_resource('description',true),	
		);
		$this->set('filterField',$filterField);
		
	}	
	
	function admin_lists(){
			
		//get data
		//lay condition, order, page, sort, filter... tu trong session de chuan bi cho ham phan trang  		
		$this->paginate=$this->Session->read($this->uses[0].'2');
		
		//lay du lieu va phan trang.		
		$data0=$this->paginate($this->uses[0]);
		
		$tree=$this->{$this->uses[0]}->generateTreeList(null,'{n}.'.$this->uses[0].'.id','{n}.'.$this->uses[0].'.key','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		//t($tree);
		foreach($data0 as $k=>$v){
			$data0[$k][$this->uses[0]]['key']=$tree[$data0[$k][$this->uses[0]]['id']];			
		}
		
		$this->set('data0',$data0);
		//render view
		$this->render('admin_lists','ajax');
		
	}
	
	function admin_add(){
		$this->{$this->uses[0]}->recursive=0;
		$name_error=_resource('can not be null',true);
		
		$isInsert=false;
		if(!empty($this->data)){
			if($this->{$this->uses[0]}->create($this->data) && $this->{$this->uses[0]}->validates()){
				
										
					if(!($this->data[$this->uses[0]]['parent_id']))$this->data[$this->uses[0]]['parent_id']=1;
					$this->data[$this->uses[0]]['key']=htmlspecialchars($this->data[$this->uses[0]]['key']);						
					if($this->{$this->uses[0]}->save($this->data)) {
						$this->data=null;
						$isInsert=true;
						$this->Session->write('message',_resource('insert data successful',true));					
					}
					else {
						$this->Session->write('error',_resource('can not insert data',true));
					}
				
			}
			else{
				$this->Session->write('error',_resource('red fill is require',true));
			}
		}
		//get tree
		$tree=$this->{$this->uses[0]}->find('list',array('conditions'=>array($this->uses[0].'.parent_id'=>1),'fields'=>array('id','key')));
		$this->set('tree',$tree);
		$this->set('isInsert',$isInsert);
		$this->set('name_error',$name_error);
		
	}

	function admin_edit($id=null){
		$this->{$this->uses[0]}->recursive=0;
		$name_error=_resource('can not be null',true);
		
		$isInsert=false;		
		if(!empty($this->data)){
			//t($this->data);
			if($this->{$this->uses[0]}->create($this->data) && $this->{$this->uses[0]}->validates()){
									
						if(!($this->data[$this->uses[0]]['parent_id']))$this->data[$this->uses[0]]['parent_id']=1;	
						$this->data[$this->uses[0]]['key']=htmlspecialchars($this->data[$this->uses[0]]['key']);						
						if($this->{$this->uses[0]}->save($this->data)) {	
							$this->{$this->uses[0]}->recover('parent');
							$this->data=null;
							$isInsert=true;							
							$this->Session->write('message',_resource('update data successful',true));						
						} else {
							$this->Session->write('error',_resource('can not update data',true));
						}
					
			}else{
				$this->Session->write('error',_resource('red fill is require',true));
			}
		}else{
			if($id!=null){
				$this->data=$this->{$this->uses[0]}->findById($id);	
				if(empty($this->data)){		
					$this->Session->write('error',_resource('data not found',true));			
					$this->render('admin_edit_not_found');
				}
			}
			else{				
				$this->Session->write('error',_resource('data not found',true));		
				$this->render('editNotFound');
			}
		}		
		//get tree
		$tree=$this->{$this->uses[0]}->find('list',array('conditions'=>array($this->uses[0].'.parent_id'=>1),'fields'=>array('id','key')));
		$this->set('tree',$tree);
		$this->set('isInsert',$isInsert);
		$this->set('name_error',$name_error);
		
	}
	
function admin_save(){
		if(isset($this->params['form']['order'])){			
			foreach($this->params['form']['order'] as $k=>$v){
				if(intval($v)){
					//set view order for category
					$this->{$this->uses[0]}->updateAll(array($this->uses[0].'.view_order'=>intval($v)),array($this->uses[0].'.id'=>$k));
				}
			}
		}
		$this->Session->write('message',_resource('view order has been update',true));
		$this->admin_lists();
	}

	function admin_delete($id = null) {		
		$this->Session->delete('error');
		//if is submit:		
		if($this->request->is('post')){
			$id_arr=$this->data['check'];
		} else {
			$id_arr=null;
		}
		//if url
		if(intval($id)){
			$id_arr[$id]='';
		}
		if(count($id_arr)>0){
			//Xoa nhieu record
			$this->{$this->uses[0]}->query('delete from '.$this->{$this->uses[0]}->tablePrefix.$this->{$this->uses[0]}->table.' where id in ('.implode(',',array_keys($id_arr)).')' );			
		} else {
			$this->Session->write('error',_resource('no information to delete',true));
		}
		$this->admin_lists();		
	}
	
function admin_moveUp($id=null){
		if(intval($id)){			
			$this->{$this->uses[0]}->moveUp($id);		
		}
		$this->admin_lists();
	}
	
	function admin_moveDown($id=null){
		if(intval($id)){			
			$this->{$this->uses[0]}->moveDown($id);		
		}
		$this->admin_lists();
	}
	
	function admin_published($id=null,$value=1,$field=0){
		$id_arr=null;
		$all=false;		
		switch($field){
			case 1: $fieldname='best';break;
			case 2: $fieldname='bestbuy';break;
			default: $fieldname='published';
		}
		if((!intval($value) && $value!=0) || intval($value)>1){
			$value=1;
		}
		
		//if is submit:		
		if($this->request->is('post')){
			$id_arr=$this->data['check'];
			$all=true;
		} 
		
		//if url
		if(intval($id) && !$all){
			$id_arr[$id]='';
		}
		
		if(count($id_arr)>0){			
			$this->{$this->uses[0]}->query('update '.$this->{$this->uses[0]}->tablePrefix.$this->{$this->uses[0]}->table.' set '.$fieldname.'='.$value.' where id in ('.implode(',',array_keys($id_arr)).')' );
		}
		if (!$all) {			
			$this->set('value',$value);
			$this->set('field',$field);
			$this->set('fieldname',$fieldname);
			$this->set('id',intval($id));
			$this->render('admin_published','ajax');		
		} else {
			$this->admin_lists();
		}
		
	}
	
	function admin_sort($sortField=null,$sortDir='asc'){
		if($sortField){
			if($sortDir!='desc') $sortDir='asc';
			$this->Session->write($this->uses[0].'2'.'.sortField',$sortField);
			$this->Session->write($this->uses[0].'2'.'.sortDir',$sortDir);
			$this->Session->write($this->uses[0].'2'.'.order',$sortField.' '.$sortDir);
		}
		$this->admin_lists();
	}
	
	function admin_page($page=1){
		$this->Session->write($this->uses[0].'2'.'.page',$page);
		$this->admin_lists();
	}
	
	function admin_display($limit=0){				
		if(intval($limit)){
			$this->Session->write($this->uses[0].'2'.'.limit',$limit);
		}
		$this->admin_lists();
	}

	function admin_filter(){
		if($this->request->is('post')){
			$this->Session->write($this->uses[0].'2.filterText',$this->request->data['filter_text']);
			$this->Session->write($this->uses[0].'2.filterField',$this->request->data['filter_field']);
			$conditions=$this->Session->read($this->uses[0].'.conditions');
			if(!empty($this->request->data['filter_text'])){				
				$conditions[$this->request->data['filter_field'].' like']='%'.$this->request->data['filter_text'].'%';				
			}
			else{
				unset($conditions[$this->request->data['filter_field'].' like']);
			}
			$this->Session->write($this->uses[0].'2.conditions',$conditions);
		}
		$this->admin_lists();
	}

	function admin_config() {
		
		if(!empty($this->data)) {	
			foreach($this->data[$this->uses[0]] as $k=>$v){
				$this->Config->query('Update `'.$this->Config->tablePrefix.$this->params['controller'].'` set `value`="'.$v.'" where `key`="'.$k.'"');													
			}
			$this->Session->write('message',_resource('config update successful',true));
		}		
		$this->data = $this->Config->find('all',array('conditions'=>array($this->uses[0].'.published'=>1,$this->uses[0].'.parent_id'=>1),'order'=>'lft'));		

		foreach($this->data as $k=>$v){
			$this->data[$k]['Children']=$this->Config->find('all',array('conditions'=>array($this->uses[0].'.parent_id'=>$v['Config']['id']),'order'=>'lft'));			
		}		
		$this->set('data',$this->data);
		//$this->test($this->data,1);
		$this->render('admin_config','ajax');	
		
	}

	

}
?>
